//Ternary operator 三目操作符
def re
re = re?:'no' // null为false
assert re == 'no'
re = ''
re = re?"yes":'no'  // groovy里 ''为false
assert re == 'no'

re = 'haha'
re = re?:'woqu' //默认不改变
assert re == 'haha'

//Direct field access operator 字段直接访问操作符   .@ 取字段
def p = new Person(name:'jack',age:12)
assert p.@name == 'jack'

// Method reference operator 方法索引操作符 .& 取方法
re = 'hello'
def sup = re.&toUpperCase
re = sup.call()
assert re == 'HELLO' 
re = sup()  //方法对象的调用有2种形式
assert re == 'HELLO'

//Spread operator * 展开提取操作符 ,在每一个元素上做某些操作
def ps = []
ps.add new Person(name:'jack',age:12)
ps.add new Person(name:'tom',age:20)
def ns = ps*.name  // 实际调用的是getName()方法
println ns   // [jack, tom]
ps*.say()	//这样简单 你让for循环的脸放哪呢。


class Person {
	def name;
	def age;
	def getName(){
		"name---$name"
	}
	String toString(){
		"my name is $name, and age is $age."
	}
	def say(){
		println this
	}
}

// 快速提取元素
def args = [4,5,6]
def list = [1,2,3,*args,7]
assert list == [1, 2, 3, 4, 5, 6, 7]

int add(int ... num) {
	def total=0
    for (i in num)
        total += i
    return total //return 可以省略
}

assert add(*args) == 15
assert add(1,*args,2) == 18

// Spread map elements *:
def m1 = [c:3, d:4]
def map = [a:1, b:2, *:m1]
assert map == [a:1, b:2, c:3, d:4]

//Range operator ..  范围操作符
def range = 0..5
assert (0..5) == [0, 1, 2, 3, 4, 5]
assert (0..<5) == [0, 1, 2, 3, 4]
assert (0..5) instanceof List
assert (0..5).size() == 6
assert ('a'..'d')  == ['a','b','c','d']

//Spaceship operator <=>  代表compareTo 方法
assert (1 <=> 1) == 0
assert (1 <=> 2) == -1
assert (2 <=> 1) == 1
assert ('a' <=> 'z') == -1

//Subscript operator 标注 下标操作符 []
list = [0,1,2,3,4]
assert list[2] == 2
assert list[0..2] == [0,1,2]
list[0..2] = [6,6,6]
assert list == [6,6,6,3,4]

// 下标操作符 的高级用法
class User {
    Long id
    String name
    def getAt(int i) {
        switch (i) {
            case 0: return id
            case 1: return name
        }
    }
    void putAt(int i, def value) {
        switch (i) {
            case 0: id = value; return
            case 1: name = value; return
        }
    }
}
def user = new User(id: 1, name: 'Alex')
assert user[0] == 1   // getAt(0) 得到 id
assert user[1] == 'Alex' // getAt(1) 得到 name
user[1] = 'Bob'    // putAt(1)
assert user.name == 'Bob'

//Membership operator  成员操作符
list = ['Grace','Rob','Emmy']
assert ('Emmy' in list) 

//Identity operator 等价操作符
//In Groovy, using == to test equality is different from using the same operator in Java. In Groovy, it is calling equals. If you want to compare reference equality, you should use is like in the following example:
// Groovy中 ==表示内容相同，is 表示地址相同
def list1 = ['Groovy 1.8','Groovy 2.0','Groovy 2.3']        
def list2 = ['Groovy 1.8','Groovy 2.0','Groovy 2.3']        
assert list1 == list2                                       
assert !list1.is(list2)  

//Coercion operator 强制转换操作符,通过asType 方法生成一个新的对象
Integer x = 123
String s = x as String
assert s == '123'

// 数组和List的差别
String[] arrStr = ['Ananas', 'Banana', 'Kiwi']  
assert arrStr instanceof String[]    
assert !(arrStr instanceof List)     

def numArr = [1, 2, 3] as int[]      
assert numArr instanceof int[]       
assert numArr.size() == 3

//Call operator 调用操作符
class MyCallable {
	def call(int x) {           
		2*x
	}
}
def mc = new MyCallable()
assert mc.call(2) == 4          
assert mc(2) == 4               

//Operator overloading 操作符重载
class Bucket {
    int size

    Bucket(int size) { this.size = size }

    Bucket plus(Bucket other) {                     
        return new Bucket(this.size + other.size)
    }
}
def b1 = new Bucket(4)
def b2 = new Bucket(11)
assert (b1 + b2).size == 15      



